package com.itheima.store.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.store.dao.ProductDao;
import com.itheima.store.entity.PageBean;
import com.itheima.store.entity.Product;
import com.itheima.store.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ProductServiceImpl implements ProductService {

    @Autowired
    private ProductDao productDao;

    @Override
    public List<Product> findNew() {
        return productDao.findNew();
    }

    @Override
    public List<Product> findHot() {
        return productDao.findHot();
    }

    @Override
    public Product getProductById(String pid) {

        return productDao.getProductById(pid);

    }

    public PageBean<Product> getPageInfo(String cid ,int currentPage,int pageSize){
        Page<Object> objects = PageHelper.startPage(currentPage, pageSize);
        List<Product> products = productDao.pageAll(cid);

        PageBean pageBean = new PageBean();

        pageBean.setList(products);
        pageBean.setCurrentPage(currentPage);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalCount((int) productDao.totalCount(cid));
        pageBean.setTotalPage(objects.getPages());
        return pageBean;

    }

    @Override
    public PageBean<Product> findByPage(int currPage, int pageSize) {
        Page<Object> objects = PageHelper.startPage(currPage, pageSize);
        List<Product> products = productDao.findAll();

        PageBean pageBean = new PageBean();

        pageBean.setList(products);
        pageBean.setCurrentPage(currPage);
        pageBean.setPageSize(pageSize);
        pageBean.setTotalCount(products.size());
        pageBean.setTotalPage(objects.getPages());
        return pageBean;
    }

    @Override
    public boolean addProduct(Product product) {

        return productDao.addProduct(product)>0?true:false;
    }
}
